<?php
class userModel extends Model{
    //设置登录
    public function setLogin($username,$password){
        $condition['username'] = $username;
        $condition['password'] = md5($password);
        $rel = $this->model->table('passport')->where($condition)->field('locks,id,username')->find(); 
        if(!$rel){
            return 0;  //帐号不存在或密码错误
        }
        if($rel['locks'] === 1){
            return -1;  //帐号被锁定
        }  
        $data = array();
        $data['login_ip'] = get_client_ip();
        $data['login_time'] = time();
        $this->model->table('passport')->where(array('id'=>$rel['id']))->data($data)->update(); 
        $cookiekey = md5(config('APP_COOKIE'));
        setcookie("uid", cp_encode($rel['id'],$cookiekey), time() + 3600,'/');
        setcookie("uname", cp_encode($rel['name'].time(),$cookiekey), time() + 3600,'/');
        return TRUE;
    }
    //判断是否登录
    public function islogin(){
        if(!isset($_COOKIE['uid'])) {
            return FALSE;
        }
        $cookiekey = md5(config('APP_COOKIE'));
        $fid = cp_decode($_COOKIE['uid'],$cookiekey);
        $condition['id'] = $fid;
        return $this->model->table('passport')->where($condition)->find(); 
    }
    //注销
    public function logout() {
        setcookie("uid", "", time() - 3600, '/');
        setcookie("uname", "", time() - 3600,'/');
        unset ($_COOKIE['uid']);
        unset ($_COOKIE['uname']);
    }  
}